<template>
  <div class="nodeItem" :style="nodeCoordinate">
    <div class="nodeHeader" :style="setHeaderColor(node.type)" :id="node.uiId">
      <span
        ><i @click="show = !show" class="el-icon-circle-plus pointer"></i>
        {{ node.english }}({{ node.chinese }})</span
      >
    </div>

    <transition name="fade">
      <div id="show" v-if="show">
        <div class="select_fields">
          <div class="content">
            <span>已选字段(21)</span>
            <i class="el-icon-setting pointer"></i>
          </div>
        </div>
        <el-input
          placeholder="请输入"
          suffix-icon="el-icon-search"
          v-model="formData['name']"
          size="small"
          @input="handleInput"
        >
        </el-input>
        <ul :id="`${node.id}${minus}cols`" class="cols" @scroll="showOut">
          <li
            class="col"
            v-for="col in node.columns.filter(
              (data) => !formData.name || data.name.includes(formData.name)
            )"
            :key="`${node.name}${minus}${col.name}`"
            :id="`${node.name}${minus}${col.name}`"
            @mouseover="handleMouseover(node, col)"
            @mouseout="handleMouseout(node, col)"
          >
            <span>{{ col.name }}</span>
            <i class="el-icon-setting pointer"></i>
          </li>
        </ul>
        <noData v-if="showNoData" height="auto"></noData>
      </div>
    </transition>
  </div>
</template>

<script>
import { tbType } from "../config/tbType";
import noData from "@/components/common/noData";
import $ from "jquery";

export default {
  name: "NodeItem",
  components: { noData },
  props: {
    node: Object,
    jsPlumb: {
      default: null,
    },
  },
  data() {
    return {
      tbType: tbType,
      minus: "-", //表名和列名的分割符号
      formData: {},
      showNoData: false,
      show: true,
    };
  },
  methods: {
    showOut() {
      // console.log(123);
      // this.jsPlumb.setSuspendDrawing(false, true);
    },
    setHeaderColor(type) {
      for (let key in tbType) {
        if (type === key) {
          return {
            backgroundColor: tbType[key].color,
          };
        }
      }
    },
    // 过滤数据
    handleInput() {
      let columns = this.node.columns.filter(
        (data) => !this.formData.name || data.name.includes(this.formData.name)
      );
      this.showNoData = !columns.length;
      // this.$nextTick(()=>{
      //     this.$parent.init();
      // });
    },
    handleMouseover(node, col) {
      let p = `${node.id}${this.minus}cols`,
        p1 = `${node.id}${this.minus}${col.name}`;

      $("#" + p)
        .find("#" + p1)
        .css("background-color", tbType["HighLight"].color);
    },
    handleMouseout(node, col) {
      let p = `${node.id}${this.minus}cols`,
        p1 = `${node.id}${this.minus}${col.name}`;

      $("#" + p)
        .find("#" + p1)
        .css("background-color", tbType["NormalLight"].color);
    },
  },
  computed: {
    nodeCoordinate: {
      get() {
        return {
          top: this.node.top + "px",
          left: this.node.left + "px",
        };
      },
    },
  },
};
</script>

<style lang="less" scoped>
.nodeItem {
  position: absolute;
  // display: felx;
  // cursor: move;
  border: 1px solid #b7b6b6;
  // border-top-left-radius: 3px;
  // border-top-right-radius: 3px;
  justify-items: center;
  align-items: center;
  z-index: 9995;
  width: 240px;
  // .title {
  //   text-align: left;
  //   padding: 0 12px;
  //   cursor: move;
  // }
  .nodeHeader {
    // border-top-left-radius: 3px;
    // border-top-right-radius: 3px;
    padding: 10px;
    background-color: #91c051;
    color: white;
    font-size: 12px;
    display: flex;
    cursor: move;
  }
  .col {
    list-style: none;
    background-color: #fff;
    padding: 8px 10px;
    font-size: 12px;
    width: auto;
    /*行内文本太长后面的就省略*/
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;

    // &:hover{
    //   background-color: #c1e4e9;
    // }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.cols {
  max-height: 200px;
  overflow-y: auto;
}

.select_fields {
  background-color: #f5f7fa;
  .content {
    padding: 8px 10px;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>